home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / depth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  19.6 KB  |  921 lines

  1. /* $Id: depth.c,v 1.8 1997/02/27 19:58:52 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.2
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: depth.c,v $
  26.  * Revision 1.8  1997/02/27 19:58:52  brianp
  27.  * don't try to clear depth buffer if there isn't one
  28.  *
  29.  * Revision 1.7  1997/01/31 23:33:08  brianp
  30.  * replaced calloc with malloc in gl_alloc_depth_buffer()
  31.  *
  32.  * Revision 1.6  1996/11/04 01:42:07  brianp
  33.  * multiply Viewport.Sz and .Tz by DEPTH_SCALE
  34.  *
  35.  * Revision 1.5  1996/10/09 03:07:25  brianp
  36.  * replaced malloc with calloc in gl_alloc_depth_buffer()
  37.  *
  38.  * Revision 1.4  1996/09/27 01:24:58  brianp
  39.  * added missing default cases to switches
  40.  *
  41.  * Revision 1.3  1996/09/19 00:54:05  brianp
  42.  * added missing returns after some gl_error() calls
  43.  *
  44.  * Revision 1.2  1996/09/15 14:19:16  brianp
  45.  * now use GLframebuffer and GLvisual
  46.  *
  47.  * Revision 1.1  1996/09/13 01:38:16  brianp
  48.  * Initial revision
  49.  *
  50.  */
  51.  
  52.  
  53.  
  54. /*
  55.  * Depth buffer functions
  56.  */
  57.  
  58.  
  59.  
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include "context.h"
  63. #include "depth.h"
  64. #include "dlist.h"
  65. #include "macros.h"
  66. #include "types.h"
  67.  
  68.  
  69.  
  70. /**********************************************************************/
  71. /*****                          API Functions                     *****/
  72. /**********************************************************************/
  73.  
  74.  
  75.  
  76. void gl_ClearDepth( GLcontext* ctx, GLclampd depth )
  77. {
  78.    if (INSIDE_BEGIN_END(ctx)) {
  79.       gl_error( ctx, GL_INVALID_OPERATION, "glClearDepth" );
  80.       return;
  81.    }
  82.    ctx->Depth.Clear = (GLfloat) CLAMP( depth, 0.0, 1.0 );
  83. }
  84.  
  85.  
  86.  
  87. void gl_DepthFunc( GLcontext* ctx, GLenum func )
  88. {
  89.    if (INSIDE_BEGIN_END(ctx)) {
  90.       gl_error( ctx, GL_INVALID_OPERATION, "glDepthFunc" );
  91.       return;
  92.    }
  93.  
  94.    switch (func) {
  95.       case GL_NEVER:
  96.       case GL_LESS:    /* (default) pass if incoming z < stored z */
  97.       case GL_GEQUAL:
  98.       case GL_LEQUAL:
  99.       case GL_GREATER:
  100.       case GL_NOTEQUAL:
  101.       case GL_EQUAL:
  102.       case GL_ALWAYS:
  103.          ctx->Depth.Func = func;
  104.          ctx->NewState |= NEW_RASTER_OPS;
  105.          break;
  106.       default:
  107.          gl_error( ctx, GL_INVALID_ENUM, "glDepth.Func" );
  108.    }
  109. }
  110.  
  111.  
  112.  
  113. void gl_DepthMask( GLcontext* ctx, GLboolean flag )
  114. {
  115.    if (INSIDE_BEGIN_END(ctx)) {
  116.       gl_error( ctx, GL_INVALID_OPERATION, "glDepthMask" );
  117.       return;
  118.    }
  119.  
  120.    /*
  121.     * GL_TRUE indicates depth buffer writing is enabled (default)
  122.     * GL_FALSE indicates depth buffer writing is disabled
  123.     */
  124.    ctx->Depth.Mask = flag;
  125.    ctx->NewState |= NEW_RASTER_OPS;
  126. }
  127.  
  128.  
  129.  
  130. void gl_DepthRange( GLcontext* ctx, GLclampd nearval, GLclampd farval )
  131. {
  132.    /*
  133.     * nearval - specifies mapping of the near clipping plane to window
  134.     *   coordinates, default is 0
  135.     * farval - specifies mapping of the far clipping plane to window
  136.     *   coordinates, default is 1
  137.     *
  138.     * After clipping and div by w, z coords are in -1.0 to 1.0,
  139.     * corresponding to near and far clipping planes.  glDepthRange
  140.     * specifies a linear mapping of the normalized z coords in
  141.     * this range to window z coords.
  142.     */
  143.  
  144.    GLfloat n, f;
  145.  
  146.    if (INSIDE_BEGIN_END(ctx)) {
  147.       gl_error( ctx, GL_INVALID_OPERATION, "glDepthRange" );
  148.       return;
  149.    }
  150.  
  151.    n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
  152.    f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
  153.  
  154.    ctx->Viewport.Near = n;
  155.    ctx->Viewport.Far = f;
  156.    ctx->Viewport.Sz = DEPTH_SCALE * ((f - n) / 2.0);
  157.    ctx->Viewport.Tz = DEPTH_SCALE * ((f - n) / 2.0 + n);
  158. }
  159.  
  160.  
  161.  
  162. /**********************************************************************/
  163. /*****                   Depth Testing Functions                  *****/
  164. /**********************************************************************/
  165.  
  166.  
  167. /*
  168.  * Depth test horizontal spans of fragments.  These functions are called
  169.  * via ctx->Driver.depth_test_span only.
  170.  *
  171.  * Input:  n - number of pixels in the span
  172.  *         x, y - location of leftmost pixel in span in window coords
  173.  *         z - array [n] of integer depth values
  174.  * In/Out:  mask - array [n] of flags (1=draw pixel, 0=don't draw) 
  175.  * Return:  number of pixels which passed depth test
  176.  */
  177.  
  178.  
  179. /*
  180.  * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
  181.  */
  182. GLuint gl_depth_test_span_generic( GLcontext* ctx,
  183.                                    GLuint n, GLint x, GLint y,
  184.                                    const GLdepth z[],
  185.                                    GLubyte mask[] )
  186. {
  187.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  188.    GLubyte *m = mask;
  189.    GLuint i;
  190.    GLuint passed = 0;
  191.  
  192.    /* switch cases ordered from most frequent to less frequent */
  193.    switch (ctx->Depth.Func) {
  194.       case GL_LESS:
  195.          if (ctx->Depth.Mask) {
  196.         /* Update Z buffer */
  197.         for (i=0; i<n; i++,zptr++,m++) {
  198.            if (*m) {
  199.           if (z[i] < *zptr) {
  200.              /* pass */
  201.              *zptr = z[i];
  202.              passed++;
  203.           }
  204.           else {
  205.              /* fail */
  206.              *m = 0;
  207.           }
  208.            }
  209.         }
  210.      }
  211.      else {
  212.         /* Don't update Z buffer */
  213.         for (i=0; i<n; i++,zptr++,m++) {
  214.            if (*m) {
  215.           if (z[i] < *zptr) {
  216.              /* pass */
  217.              passed++;
  218.           }
  219.           else {
  220.              *m = 0;
  221.           }
  222.            }
  223.         }
  224.      }
  225.      break;
  226.       case GL_LEQUAL:
  227.      if (ctx->Depth.Mask) {
  228.         /* Update Z buffer */
  229.         for (i=0;i<n;i++,zptr++,m++) {
  230.            if (*m) {
  231.           if (z[i] <= *zptr) {
  232.              *zptr = z[i];
  233.              passed++;
  234.           }
  235.           else {
  236.              *m = 0;
  237.           }
  238.            }
  239.         }
  240.      }
  241.      else {
  242.         /* Don't update Z buffer */
  243.         for (i=0;i<n;i++,zptr++,m++) {
  244.            if (*m) {
  245.           if (z[i] <= *zptr) {
  246.              /* pass */
  247.              passed++;
  248.           }
  249.           else {
  250.              *m = 0;
  251.           }
  252.            }
  253.         }
  254.      }
  255.      break;
  256.       case GL_GEQUAL:
  257.      if (ctx->Depth.Mask) {
  258.         /* Update Z buffer */
  259.         for (i=0;i<n;i++,zptr++,m++) {
  260.            if (*m) {
  261.           if (z[i] >= *zptr) {
  262.              *zptr = z[i];
  263.              passed++;
  264.           }
  265.           else {
  266.              *m = 0;
  267.           }
  268.            }
  269.         }
  270.      }
  271.      else {
  272.         /* Don't update Z buffer */
  273.         for (i=0;i<n;i++,zptr++,m++) {
  274.            if (*m) {
  275.           if (z[i] >= *zptr) {
  276.              /* pass */
  277.              passed++;
  278.           }
  279.           else {
  280.              *m = 0;
  281.           }
  282.            }
  283.         }
  284.      }
  285.      break;
  286.       case GL_GREATER:
  287.      if (ctx->Depth.Mask) {
  288.         /* Update Z buffer */
  289.         for (i=0;i<n;i++,zptr++,m++) {
  290.            if (*m) {
  291.           if (z[i] > *zptr) {
  292.              *zptr = z[i];
  293.              passed++;
  294.           }
  295.           else {
  296.              *m = 0;
  297.           }
  298.            }
  299.         }
  300.      }
  301.      else {
  302.         /* Don't update Z buffer */
  303.         for (i=0;i<n;i++,zptr++,m++) {
  304.            if (*m) {
  305.           if (z[i] > *zptr) {
  306.              /* pass */
  307.              passed++;
  308.           }
  309.           else {
  310.              *m = 0;
  311.           }
  312.            }
  313.         }
  314.      }
  315.      break;
  316.       case GL_NOTEQUAL:
  317.      if (ctx->Depth.Mask) {
  318.         /* Update Z buffer */
  319.         for (i=0;i<n;i++,zptr++,m++) {
  320.            if (*m) {
  321.           if (z[i] != *zptr) {
  322.              *zptr = z[i];
  323.              passed++;
  324.           }
  325.           else {
  326.              *m = 0;
  327.           }
  328.            }
  329.         }
  330.      }
  331.      else {
  332.         /* Don't update Z buffer */
  333.         for (i=0;i<n;i++,zptr++,m++) {
  334.            if (*m) {
  335.           if (z[i] != *zptr) {
  336.              /* pass */
  337.              passed++;
  338.           }
  339.           else {
  340.              *m = 0;
  341.           }
  342.            }
  343.         }
  344.      }
  345.      break;
  346.       case GL_EQUAL:
  347.      if (ctx->Depth.Mask) {
  348.         /* Update Z buffer */
  349.         for (i=0;i<n;i++,zptr++,m++) {
  350.            if (*m) {
  351.           if (z[i] == *zptr) {
  352.              *zptr = z[i];
  353.              passed++;
  354.           }
  355.           else {
  356.              *m =0;
  357.           }
  358.            }
  359.         }
  360.      }
  361.      else {
  362.         /* Don't update Z buffer */
  363.         for (i=0;i<n;i++,zptr++,m++) {
  364.            if (*m) {
  365.           if (z[i] == *zptr) {
  366.              /* pass */
  367.              passed++;
  368.           }
  369.           else {
  370.              *m =0;
  371.           }
  372.            }
  373.         }
  374.      }
  375.      break;
  376.       case GL_ALWAYS:
  377.      if (ctx->Depth.Mask) {
  378.         /* Update Z buffer */
  379.         for (i=0;i<n;i++,zptr++,m++) {
  380.            if (*m) {
  381.           *zptr = z[i];
  382.           passed++;
  383.            }
  384.         }
  385.      }
  386.      else {
  387.         /* Don't update Z buffer or mask */
  388.         passed = n;
  389.      }
  390.      break;
  391.       case GL_NEVER:
  392.      for (i=0;i<n;i++) {
  393.         mask[i] = 0;
  394.      }
  395.      break;
  396.       default:
  397.          abort();
  398.    } /*switch*/
  399.  
  400.    return passed;
  401. }
  402.  
  403.  
  404.  
  405. /*
  406.  * glDepthFunc(GL_LESS) and glDepthMask(GL_TRUE).
  407.  */
  408. GLuint gl_depth_test_span_less( GLcontext* ctx,
  409.                                 GLuint n, GLint x, GLint y, const GLdepth z[],
  410.                                 GLubyte mask[] )
  411. {
  412.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  413.    GLuint i;
  414.    GLuint passed = 0;
  415.  
  416.    for (i=0; i<n; i++) {
  417.       if (mask[i]) {
  418.          if (z[i] < zptr[i]) {
  419.             /* pass */
  420.             zptr[i] = z[i];
  421.             passed++;
  422.          }
  423.          else {
  424.             /* fail */
  425.             mask[i] = 0;
  426.          }
  427.       }
  428.    }
  429.    return passed;
  430. }
  431.  
  432.  
  433. /*
  434.  * glDepthFunc(GL_GREATER) and glDepthMask(GL_TRUE).
  435.  */
  436. GLuint gl_depth_test_span_greater( GLcontext* ctx,
  437.                                    GLuint n, GLint x, GLint y,
  438.                                    const GLdepth z[],
  439.                                    GLubyte mask[] )
  440. {
  441.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  442.    GLuint i;
  443.    GLuint passed = 0;
  444.  
  445.    for (i=0; i<n; i++) {
  446.       if (mask[i]) {
  447.          if (z[i] > zptr[i]) {
  448.             /* pass */
  449.             zptr[i] = z[i];
  450.             passed++;
  451.          }
  452.          else {
  453.             /* fail */
  454.             mask[i] = 0;
  455.          }
  456.       }
  457.    }
  458.    return passed;
  459. }
  460.  
  461.  
  462.  
  463. /*
  464.  * Depth test an array of randomly positioned fragments.
  465.  */
  466.  
  467.  
  468. #define ZADDR_SETUP   GLdepth *depthbuffer = ctx->Buffer->Depth; \
  469.                       GLint width = ctx->Buffer->Width;
  470.  
  471. #define ZADDR( X, Y )   (depthbuffer + (Y) * width + (X) )
  472.  
  473.  
  474.  
  475. /*
  476.  * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
  477.  */
  478. void gl_depth_test_pixels_generic( GLcontext* ctx,
  479.                                    GLuint n, const GLint x[], const GLint y[],
  480.                                    const GLdepth z[], GLubyte mask[] )
  481. {
  482.    register GLdepth *zptr;
  483.    register GLuint i;
  484.  
  485.    /* switch cases ordered from most frequent to less frequent */
  486.    switch (ctx->Depth.Func) {
  487.       case GL_LESS:
  488.          if (ctx->Depth.Mask) {
  489.         /* Update Z buffer */
  490.         for (i=0; i<n; i++) {
  491.            if (mask[i]) {
  492.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  493.           if (z[i] < *zptr) {
  494.              /* pass */
  495.              *zptr = z[i];
  496.           }
  497.           else {
  498.              /* fail */
  499.              mask[i] = 0;
  500.           }
  501.            }
  502.         }
  503.      }
  504.      else {
  505.         /* Don't update Z buffer */
  506.         for (i=0; i<n; i++) {
  507.            if (mask[i]) {
  508.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  509.           if (z[i] < *zptr) {
  510.              /* pass */
  511.           }
  512.           else {
  513.              /* fail */
  514.              mask[i] = 0;
  515.           }
  516.            }
  517.         }
  518.      }
  519.      break;
  520.       case GL_LEQUAL:
  521.          if (ctx->Depth.Mask) {
  522.         /* Update Z buffer */
  523.         for (i=0; i<n; i++) {
  524.            if (mask[i]) {
  525.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  526.           if (z[i] <= *zptr) {
  527.              /* pass */
  528.              *zptr = z[i];
  529.           }
  530.           else {
  531.              /* fail */
  532.              mask[i] = 0;
  533.           }
  534.            }
  535.         }
  536.      }
  537.      else {
  538.         /* Don't update Z buffer */
  539.         for (i=0; i<n; i++) {
  540.            if (mask[i]) {
  541.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  542.           if (z[i] <= *zptr) {
  543.              /* pass */
  544.           }
  545.           else {
  546.              /* fail */
  547.              mask[i] = 0;
  548.           }
  549.            }
  550.         }
  551.      }
  552.      break;
  553.       case GL_GEQUAL:
  554.          if (ctx->Depth.Mask) {
  555.         /* Update Z buffer */
  556.         for (i=0; i<n; i++) {
  557.            if (mask[i]) {
  558.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  559.           if (z[i] >= *zptr) {
  560.              /* pass */
  561.              *zptr = z[i];
  562.           }
  563.           else {
  564.              /* fail */
  565.              mask[i] = 0;
  566.           }
  567.            }
  568.         }
  569.      }
  570.      else {
  571.         /* Don't update Z buffer */
  572.         for (i=0; i<n; i++) {
  573.            if (mask[i]) {
  574.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  575.           if (z[i] >= *zptr) {
  576.              /* pass */
  577.           }
  578.           else {
  579.              /* fail */
  580.              mask[i] = 0;
  581.           }
  582.            }
  583.         }
  584.      }
  585.      break;
  586.       case GL_GREATER:
  587.          if (ctx->Depth.Mask) {
  588.         /* Update Z buffer */
  589.         for (i=0; i<n; i++) {
  590.            if (mask[i]) {
  591.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  592.           if (z[i] > *zptr) {
  593.              /* pass */
  594.              *zptr = z[i];
  595.           }
  596.           else {
  597.              /* fail */
  598.              mask[i] = 0;
  599.           }
  600.            }
  601.         }
  602.      }
  603.      else {
  604.         /* Don't update Z buffer */
  605.         for (i=0; i<n; i++) {
  606.            if (mask[i]) {
  607.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  608.           if (z[i] > *zptr) {
  609.              /* pass */
  610.           }
  611.           else {
  612.              /* fail */
  613.              mask[i] = 0;
  614.           }
  615.            }
  616.         }
  617.      }
  618.      break;
  619.       case GL_NOTEQUAL:
  620.          if (ctx->Depth.Mask) {
  621.         /* Update Z buffer */
  622.         for (i=0; i<n; i++) {
  623.            if (mask[i]) {
  624.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  625.           if (z[i] != *zptr) {
  626.              /* pass */
  627.              *zptr = z[i];
  628.           }
  629.           else {
  630.              /* fail */
  631.              mask[i] = 0;
  632.           }
  633.            }
  634.         }
  635.      }
  636.      else {
  637.         /* Don't update Z buffer */
  638.         for (i=0; i<n; i++) {
  639.            if (mask[i]) {
  640.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  641.           if (z[i] != *zptr) {
  642.              /* pass */
  643.           }
  644.           else {
  645.              /* fail */
  646.              mask[i] = 0;
  647.           }
  648.            }
  649.         }
  650.      }
  651.      break;
  652.       case GL_EQUAL:
  653.          if (ctx->Depth.Mask) {
  654.         /* Update Z buffer */
  655.         for (i=0; i<n; i++) {
  656.            if (mask[i]) {
  657.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  658.           if (z[i] == *zptr) {
  659.              /* pass */
  660.              *zptr = z[i];
  661.           }
  662.           else {
  663.              /* fail */
  664.              mask[i] = 0;
  665.           }
  666.            }
  667.         }
  668.      }
  669.      else {
  670.         /* Don't update Z buffer */
  671.         for (i=0; i<n; i++) {
  672.            if (mask[i]) {
  673.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  674.           if (z[i] == *zptr) {
  675.              /* pass */
  676.           }
  677.           else {
  678.              /* fail */
  679.              mask[i] = 0;
  680.           }
  681.            }
  682.         }
  683.      }
  684.      break;
  685.       case GL_ALWAYS:
  686.      if (ctx->Depth.Mask) {
  687.         /* Update Z buffer */
  688.         for (i=0; i<n; i++) {
  689.            if (mask[i]) {
  690.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  691.           *zptr = z[i];
  692.            }
  693.         }
  694.      }
  695.      else {
  696.         /* Don't update Z buffer or mask */
  697.      }
  698.      break;
  699.       case GL_NEVER:
  700.      /* depth test never passes */
  701.      for (i=0;i<n;i++) {
  702.         mask[i] = 0;
  703.      }
  704.      break;
  705.       default:
  706.          abort();
  707.    } /*switch*/
  708. }
  709.  
  710.  
  711.  
  712. /*
  713.  * glDepthFunc( GL_LESS ) and glDepthMask( GL_TRUE ).
  714.  */
  715. void gl_depth_test_pixels_less( GLcontext* ctx,
  716.                                 GLuint n, const GLint x[], const GLint y[],
  717.                                 const GLdepth z[], GLubyte mask[] )
  718. {
  719.    GLdepth *zptr;
  720.    GLuint i;
  721.  
  722.    for (i=0; i<n; i++) {
  723.       if (mask[i]) {
  724.          zptr = Z_ADDRESS(ctx,x[i],y[i]);
  725.          if (z[i] < *zptr) {
  726.             /* pass */
  727.             *zptr = z[i];
  728.          }
  729.          else {
  730.             /* fail */
  731.             mask[i] = 0;
  732.          }
  733.       }
  734.    }
  735. }
  736.  
  737.  
  738. /*
  739.  * glDepthFunc( GL_GREATER ) and glDepthMask( GL_TRUE ).
  740.  */
  741. void gl_depth_test_pixels_greater( GLcontext* ctx,
  742.                                    GLuint n, const GLint x[], const GLint y[],
  743.                                    const GLdepth z[], GLubyte mask[] )
  744. {
  745.    GLdepth *zptr;
  746.    GLuint i;
  747.  
  748.    for (i=0; i<n; i++) {
  749.       if (mask[i]) {
  750.          zptr = Z_ADDRESS(ctx,x[i],y[i]);
  751.          if (z[i] > *zptr) {
  752.             /* pass */
  753.             *zptr = z[i];
  754.          }
  755.          else {
  756.             /* fail */
  757.             mask[i] = 0;
  758.          }
  759.       }
  760.    }
  761. }
  762.  
  763.  
  764.  
  765.  
  766. /**********************************************************************/
  767. /*****                      Read Depth Buffer                     *****/
  768. /**********************************************************************/
  769.  
  770.  
  771. /*
  772.  * Return a span of depth values from the depth buffer as floats in [0,1].
  773.  * This function is only called through Driver.read_depth_span_float()
  774.  * Input:  n - how many pixels
  775.  *         x,y - location of first pixel
  776.  * Output:  depth - the array of depth values
  777.  */
  778. void gl_read_depth_span_float( GLcontext* ctx,
  779.                                GLuint n, GLint x, GLint y, GLfloat depth[] )
  780. {
  781.    GLdepth *zptr;
  782.    GLfloat scale;
  783.    GLuint i;
  784.  
  785.    scale = 1.0F / DEPTH_SCALE;
  786.  
  787.    if (ctx->Buffer->Depth) {
  788.       zptr = Z_ADDRESS( ctx, x, y );
  789.       for (i=0;i<n;i++) {
  790.      depth[i] = (GLfloat) zptr[i] * scale;
  791.       }
  792.    }
  793.    else {
  794.       for (i=0;i<n;i++) {
  795.      depth[i] = 0.0F;
  796.       }
  797.    }
  798. }
  799.  
  800.  
  801. /*
  802.  * Return a span of depth values from the depth buffer as integers in
  803.  * [0,MAX_DEPTH].
  804.  * This function is only called through Driver.read_depth_span_int()
  805.  * Input:  n - how many pixels
  806.  *         x,y - location of first pixel
  807.  * Output:  depth - the array of depth values
  808.  */
  809. void gl_read_depth_span_int( GLcontext* ctx,
  810.                              GLuint n, GLint x, GLint y, GLdepth depth[] )
  811. {
  812.    if (ctx->Buffer->Depth) {
  813.       GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  814.       MEMCPY( depth, zptr, n * sizeof(GLdepth) );
  815.    }
  816.    else {
  817.       GLuint i;
  818.       for (i=0;i<n;i++) {
  819.      depth[i] = 0.0;
  820.       }
  821.    }
  822. }
  823.  
  824.  
  825.  
  826. /**********************************************************************/
  827. /*****                Allocate and Clear Depth Buffer             *****/
  828. /**********************************************************************/
  829.  
  830.  
  831.  
  832. /*
  833.  * Allocate a new depth buffer.  If there's already a depth buffer allocated
  834.  * it will be free()'d.  The new depth buffer will be uniniitalized.
  835.  * This function is only called through Driver.alloc_depth_buffer.
  836.  */
  837. void gl_alloc_depth_buffer( GLcontext* ctx )
  838. {
  839.    /* deallocate current depth buffer if present */
  840.    if (ctx->Buffer->Depth) {
  841.       free(ctx->Buffer->Depth);
  842.       ctx->Buffer->Depth = NULL;
  843.    }
  844.  
  845.    /* allocate new depth buffer, but don't initialize it */
  846.    ctx->Buffer->Depth = (GLdepth *) malloc( ctx->Buffer->Width
  847.                                             * ctx->Buffer->Height
  848.                                             * sizeof(GLdepth) );
  849.    if (!ctx->Buffer->Depth) {
  850.       /* out of memory */
  851.       ctx->Depth.Test = GL_FALSE;
  852.       gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
  853.    }
  854. }
  855.  
  856.  
  857.  
  858.  
  859. /*
  860.  * Clear the depth buffer.  If the depth buffer doesn't exist yet we'll
  861.  * allocate it now.
  862.  * This function is only called through Driver.clear_depth_buffer.
  863.  */
  864. void gl_clear_depth_buffer( GLcontext* ctx )
  865. {
  866.    GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
  867.  
  868.    if (ctx->Visual->DepthBits==0 || !ctx->Buffer->Depth) {
  869.       /* no depth buffer */
  870.       return;
  871.    }
  872.  
  873.    /* The loops in this function have been written so the IRIX 5.3
  874.     * C compiler can unroll them.  Hopefully other compilers can too!
  875.     */
  876.  
  877.    if (ctx->Scissor.Enabled) {
  878.       /* only clear scissor region */
  879.       GLint y;
  880.       for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
  881.          GLdepth *d = Z_ADDRESS( ctx, ctx->Buffer->Xmin, y );
  882.          GLint n = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
  883.          do {
  884.             *d++ = clear_value;
  885.             n--;
  886.          } while (n);
  887.       }
  888.    }
  889.    else {
  890.       /* clear whole buffer */
  891.       if (sizeof(GLdepth)==2 && (clear_value&0xff)==(clear_value>>8)) {
  892.          /* lower and upper bytes of clear_value are same, use MEMSET */
  893.          MEMSET( ctx->Buffer->Depth, clear_value&0xff,
  894.                  2*ctx->Buffer->Width*ctx->Buffer->Height);
  895.       }
  896.       else {
  897.          GLdepth *d = ctx->Buffer->Depth;
  898.          GLint n = ctx->Buffer->Width * ctx->Buffer->Height;
  899.          while (n>=16) {
  900.             d[0] = clear_value;    d[1] = clear_value;
  901.             d[2] = clear_value;    d[3] = clear_value;
  902.             d[4] = clear_value;    d[5] = clear_value;
  903.             d[6] = clear_value;    d[7] = clear_value;
  904.             d[8] = clear_value;    d[9] = clear_value;
  905.             d[10] = clear_value;   d[11] = clear_value;
  906.             d[12] = clear_value;   d[13] = clear_value;
  907.             d[14] = clear_value;   d[15] = clear_value;
  908.             d += 16;
  909.             n -= 16;
  910.          }
  911.          while (n>0) {
  912.             *d++ = clear_value;
  913.             n--;
  914.          }
  915.       }
  916.    }
  917. }
  918.  
  919.  
  920.  
  921.